import { DayVData, DayVDataPartner } from './DayVData'
import FaultLogger from '@ohos.faultLogger'

type CalendarPageDates = Date[]

export class CalendarProvider {

  static OneDayMilliSecond: number = 60 * 60 * 24 * 1000

  static fortyTwoDays(date: Date) {
    const monthFirstDay = new Date(date.getFullYear(), date.getMonth(), 1)
    let startTime = monthFirstDay.getTime() - (monthFirstDay.getDay() * CalendarProvider.OneDayMilliSecond)
    let dates: Date[] = []
    for (let i = 0; i < 42; i++) {
      const d = new Date(startTime)
      dates.push(d)
      startTime += CalendarProvider.OneDayMilliSecond
    }
    return dates
  }


  /**
   * DayOfWeek,一周中的星期几.
   * @param year
   * @param month 1开始的月份数.
   * @returns
   */
  static getFirstDayOfWeek(year:number, month:number):number {
    return new Date(Date.UTC(year, month - 1, 1)).getDay();
  }
  //
  // static fortyTwoDays_2(dayItem:DayVData,fixedFortyTwo:boolean = true):DayVData2[]{
  //   let today = DayVDataPartner2.today();
  //   let days = [] as DayVData[];
  //   const dayOfWeek:number = this.getFirstDayOfWeek(dayItem.year, dayItem.month);
  //   //需要补充上个月的天数.
  //   let previousCount = dayOfWeek == 0 ? 0 : dayOfWeek;
  //   if(previousCount != 0) {
  //     let previousMonthDays = this.getDays(DayVDataPartner.previousMonth(dayItem),today)
  //     let previousStartIndex = previousMonthDays.length - previousCount;
  //     days.push(...previousMonthDays.filter((item,index)=>(index >= previousStartIndex)));
  //   }
  //
  //   let currentMonthDays = this.getDays(dayItem,today);
  //   days.push(...currentMonthDays);
  //
  //   let nextCount = days.length % 7 == 0 ? 0 : 7 - (days.length % 7)
  //   if(fixedFortyTwo){
  //     nextCount+= 7;
  //   }
  //   if(nextCount) {
  //     let nextMonthDays = this.getDays(DayVDataPartner.nextMonth(dayItem),today)
  //     //序号从0开始,index需要-1.
  //     let nextMonthEndIndex = nextCount - 1;
  //     days.push(...nextMonthDays.filter((item,index)=>(index <= nextMonthEndIndex)));
  //   }
  //   return days;
  //   // return [days[0]]
  // }


  /**
   *
   * @param dayItem
   * @param fixedFortyTwo 是否固定是返回42天 6周. 有的情况5周就能显示完整当月数据.
   * true,  每月都返回固定的6周.
   * false, 5周就能显示完整的只返回35个数据,否则返回42个数据.
   * @returns
   */
  static fortyTwoDays_3(dayItem:DayVData,fixedFortyTwo:boolean = true):DayVData[]{
    let today = DayVDataPartner.today();
    let days = [] as DayVData[];
    const dayOfWeek:number = this.getFirstDayOfWeek(dayItem.year, dayItem.month);
    //需要补充上个月的天数.
    let previousCount = dayOfWeek == 0 ? 0 : dayOfWeek;
    if(previousCount != 0) {
      let previousMonthDays = this.getDays(DayVDataPartner.previousMonth(dayItem),false)
      let previousStartIndex = previousMonthDays.length - previousCount;
      days.push(...previousMonthDays.filter((item,index)=>(index >= previousStartIndex)));
    }

    let currentMonthDays = this.getDays(dayItem,true);
    days.push(...currentMonthDays);

    let nextCount = days.length % 7 == 0 ? 0 : 7 - (days.length % 7)
    if(fixedFortyTwo){
      nextCount+= 7;
    }
    if(nextCount) {
      let nextMonthDays = this.getDays(DayVDataPartner.nextMonth(dayItem),false)
      //序号从0开始,index需要-1.
      let nextMonthEndIndex = nextCount - 1;
      days.push(...nextMonthDays.filter((item,index)=>(index <= nextMonthEndIndex)));
    }
    return days;
    // return [days[0]]
  }



  /**
   * 返回一个月每天的DayVData集合.
   * @param year
   * @param isCurrentMonth
   */
  static getDays(dayItem:DayVData,isCurrentMonth:boolean = false):DayVData[] {
    let days:Array<DayVData> = [];
    const thisMonthDays = CalendarProvider.getThisMonthDayCount(dayItem.year, dayItem.month);
    for (let i = 1; i <= thisMonthDays; i++) {
      let item = DayVDataPartner.generate(dayItem.year,dayItem.month,i);
      item.isCurrentMonth = isCurrentMonth
      days.push(item);
    }
    return days;
  }

  /**
   * Date对象的day传0,然后调用getDate返回的是这个月的总天数.
   * @param year
   * @param month
   * @returns
   */
  static getThisMonthDayCount(year:number, month:number):number {
    return new Date(year, month, 0).getDate();
  }


  static lastMonthSameDay(date: Date) {
    const baseYear = date.getFullYear()
    const baseMonth = date.getMonth()
    const baseDay = date.getDate()
    if (baseMonth == 0) {
      return new Date(baseYear - 1, 11, baseDay)
    } else {
      return new Date(baseYear, baseMonth - 1, baseDay)
    }
  }

  static nextMonthSameDay(date: Date) {
    const baseYear = date.getFullYear()
    const baseMonth = date.getMonth()
    const baseDay = date.getDate()
    if (baseMonth == 11) {
      return new Date(baseYear + 1, 0, baseDay)
    } else {
      return new Date(baseYear, baseMonth + 1, baseDay)
    }
  }

}